package com.hsc.ossservice.service.impl;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CannedAccessControlList;
import com.hsc.ossservice.service.OssService;
import com.hsc.ossservice.utils.ConstantPropertiesUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class OssServiceImpl implements OssService {

    /**
     * 上传头像文件到oss
     * @param file
     * @return
     */
    @Override
    public String uploadFileAvatar(MultipartFile file) {
        String endPoint = ConstantPropertiesUtil.END_POINT;
        String accessKeyId = ConstantPropertiesUtil.ACCESS_KEY_ID;
        String accessKeySecret = ConstantPropertiesUtil.ACCESS_KEY_SECRET;
        String bucketName = ConstantPropertiesUtil.BUCKET_NAME;
//        String fileHost = ConstantPropertiesUtil.FILE_HOST;
//        String uploadUrl = null;
        try {
            //判断oss实例是否存在：如果不存在则创建，如果存在则获取
            OSSClient ossClient = new OSSClient(endPoint, accessKeyId,
                    accessKeySecret);
            if (!ossClient.doesBucketExist(bucketName)) {
                //创建bucket
                ossClient.createBucket(bucketName);
                //设置oss实例的访问权限：公共读
                ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
            }
            //获取文件上传输入流
            InputStream inputStream = file.getInputStream();
            //获取文件名称
            String fileOriginalFilename = file.getOriginalFilename();
            StringBuilder filename = new StringBuilder();
            //生成随机UUID
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            //获取当前时间
            SimpleDateFormat format0 = new SimpleDateFormat("yy/MM/dd");
            String time = format0.format(new Date());
            //名称拼接时间+/+uuid+文件名称
            filename.append(time).append("/").append(uuid).append(fileOriginalFilename);
            //调用oss方法上传，第一个参数bucket名臣，第二个参数上传文件路径和名称，第三个参数上传文件输入流
            ossClient.putObject(bucketName,filename.toString(),inputStream);
            //关闭oss
            ossClient.shutdown();
            String url = "http://" + bucketName + "." + endPoint + "/" + filename;
            return url;


//            //获取上传文件流
//            InputStream inputStream = file.getInputStream();
//            //构建日期路径：avatar/2019/02/26/文件名
//            String filePath = new DateTimeLiteralExpression.DateTime().toString("yyyy/MM/dd");
//            //文件名：uuid.扩展名
//            String original = file.getOriginalFilename();
//            String fileName = UUID.randomUUID().toString();
//            String fileType = original.substring(original.lastIndexOf("."));
//            String newName = fileName + fileType;
//            String fileUrl = fileHost + "/" + filePath + "/" + newName;
//            //文件上传至阿里云
//            ossClient.putObject(bucketName, fileUrl, inputStream);
//            // 关闭OSSClient。
//            ossClient.shutdown();
//            //获取url地址
//            uploadUrl = "http://" + bucketName + "." + endPoint + "/" + fileUrl;
        } catch (Exception e) {
//            throw new GuliException(ResultCodeEnum.FILE_UPLOAD_ERROR);

        }

        return null;
    }
}
